package x.ovo.wechat.bot.core;

import cn.hutool.setting.yaml.YamlUtil;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import x.ovo.wechat.bot.common.Constant;

import java.util.Optional;

/**
 * 配置
 *
 * @author ovo, created by 2024/05/03
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class Config {

    private static Config INSTANCE;

    /** 自动登录 */
    private boolean autoLogin;
    /** 打印终端 */
    private boolean printTerminal;
    /** 打印横幅 */
    private boolean printBanner;
    /** 重试计数 */
    private int retryCount;

    public static Config get() {
        return Optional.ofNullable(INSTANCE).orElseGet(() -> {
            Config config = Optional.ofNullable(loadConfig()).orElse(defaultConfig());
            INSTANCE = config;
            return config;
        });
    }

    public static Config defaultConfig() {
        return builder().autoLogin(true).printTerminal(true).printBanner(true).retryCount(3).build();
    }

    public static Config loadConfig() {
        if (!Constant.CONFIG_FILE.exists()) {
            return null;
        }
        return YamlUtil.loadByPath(Constant.CONFIG_FILE.getPath(), Config.class);
    }
}
